package com.doubi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.doubi.entity.ResponseEntity;
import com.doubi.entity.Userinfo;
import com.doubi.service.UserinfoService;
import com.doubi.util.SMSMessage;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.*;

@RestController
@CrossOrigin("*")
public class UserinfoController {
    //用户业务逻辑层
    @Autowired
    private UserinfoService userinfoService;
    @Autowired
    MinioClient minioClient;
    /**
     * 用户登录
     */
    @PostMapping("loginUser")
    public Object loginUser(@RequestBody Userinfo userinfo) {
        System.out.println("userinfo = " + userinfo);
        QueryWrapper<Userinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("phoneNum", userinfo.getPhoneNum()).eq("password", userinfo.getPassword());
        Userinfo user = userinfoService.getOne(wrapper);
        if (user != null) {
            return user;
        }
        return null;
    }
    /**
     * userid 用户id
     * return 返回用户信息
     * 查询个人信息
     */
    @GetMapping("/selectUserInfo")
    public Object getUserinfo(Integer userid) {
        QueryWrapper<Userinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("userid", userid);
        Userinfo userinfo = userinfoService.getOne(wrapper);
        userinfo.setHeadImg("http://192.168.43.92:9000/star/"+userinfo.getHeadImg());
        return userinfo;
    }

    /**
     * 修改个人信息
     */
    @PostMapping("/saveUserInfo")
    public Object saveUserinfo(@RequestBody Userinfo userinfo) {
        QueryWrapper<Userinfo> wrapper = new QueryWrapper<>();
        //查询修改的手机号是否相同
        System.out.println("userid"+userinfo.getUserId());
        Integer count=userinfoService.selectIphone(userinfo.getPhoneNum(),userinfo.getUserId());
        if(count==0){
           return userinfoService.saveUserInfo(userinfo);
        }
        return 2 ;
    }
    /**
     * 修改头像
     */
    @PostMapping(value = "/updateImg")
    public Map updateImg(@RequestParam("file") MultipartFile file) throws Exception{
        Map map=new HashMap<>();
        String fileName= UUID.randomUUID() +file.getOriginalFilename();//上传文件名
        InputStream inputStream = file.getInputStream();
        ObjectWriteResponse bjectWriteResponse = minioClient.putObject(PutObjectArgs.builder()
                .bucket("star")
                .object(fileName)
                .stream(inputStream, -1L, 10485760L)
                .build());
        //new ResponseEntity().addDate("frilmsg",bjectWriteResponse.object());
        map.put("frilmsg",bjectWriteResponse.object());
        return map;
    }
    /**
     * 保存图片
     */
    @PostMapping("saveImg")
    public Object saveImg(String frilmsg,Integer userid) throws Exception{
        return userinfoService.saveImg(frilmsg,userid);
    }
    /**
     * 发送验证码
     */
    @PostMapping("sendCode")
    public Object sendCode(String phone) throws Exception{
        SMSMessage sms=new SMSMessage();
        Map<String,String> map=new HashMap<>();
        map.put("code",sms.sendSMS(phone));
        return map;
    }
    /**
     * 注册新账号
     */
    @PostMapping("register")
    public Object register(String phone) {
        Userinfo userinfo=new Userinfo();
        String name="用户"+UUID.randomUUID();
        userinfo.setUserName(name.substring(0,10));
        Random random=new Random();
        userinfo.setUserAccount("did"+random.nextInt(9999999));
        userinfo.setPhoneNum(phone);
        userinfo.setPassword("123456");
        userinfo.setHeadImg("140ba43f-3d7a-4340-9f43-1779153ee700file.jpeg");
        return userinfoService.save(userinfo);
    }
    /**
     * 查询用户
     */
    @GetMapping("selectUser")
    public Object selectUser(String searhMag) {
        QueryWrapper<Userinfo> wrapper = new QueryWrapper<>();
        wrapper.like("userName","%"+searhMag+"%").or().like("userAccount","%"+searhMag+"%");
        List<Userinfo> list = userinfoService.list(wrapper);
        for (Userinfo userinfo : list) {
            userinfo.setHeadImg("http://localhost:9000/star/"+userinfo.getHeadImg());
        }
        return list;
    }

    /**
     * 查询所有用户
     */
    @GetMapping("/selectAllUser")
    public ResponseEntity getAllUser(Integer pageNo, Integer pageSize, String selUserName, String selUserAccount, String selPhone, String selWeiXin) {
        return userinfoService.userConditionQuery(pageNo,pageSize,selUserName,selUserAccount,selPhone,selWeiXin);
    }

    @PostMapping("/delUserByIds")
    public String delUserById(@RequestBody List<Integer> delUserByIds) {
        int flag = userinfoService.delUserByIds(delUserByIds);
        if (flag>0){
            return "success";
        }else {
            return "fail";
        }
    }

    @PostMapping("/updUserInfo")
    public String updUserInfo(@RequestBody Userinfo userinfo) {
        System.out.println(userinfo);
        QueryWrapper<Userinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("userId",userinfo.getUserId());
        boolean b = userinfoService.update(userinfo,wrapper);
        if (b){
            return "success";
        }else {
            return "fail";
        }
    }
}
